package com.viewpagertext.activitys;

import static com.viewpagertext.activitys.MainActivity.mList;
import static com.viewpagertext.adapters.MusicGridAdapter.GroupFlag;
import static com.viewpagertext.adapters.MusicGridAdapter.MyStarredSong;
import static com.viewpagertext.adapters.MusicGridAdapter.RecentPlayFlag;
import static com.viewpagertext.adapters.MusicGridAdapter.SearchName;
import static com.viewpagertext.fragments.ListFragment.clientList;
import static com.viewpagertext.fragments.ListFragment.tasks;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.StarredSongDao;
import com.viewpagertext.R;
import com.viewpagertext.adapters.PlayFragmentPagerAdapter;
import com.viewpagertext.databinding.ActivityPlayBinding;
import com.viewpagertext.fragments.ListFragment;
import com.viewpagertext.fragments.SongFragment;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.OkHttpClient;


public class PlayActivity extends AppCompatActivity {

    private List<Fragment> mDatas;
    private ActivityPlayBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_play);

        initView();//实例化控件
    }

    private void initView() {
        initDatas();
        PlayFragmentPagerAdapter playFragmentPagerAdapter = new PlayFragmentPagerAdapter(getSupportFragmentManager(), mDatas);
        binding.playViewpager.setAdapter(playFragmentPagerAdapter);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        SearchName = null;
        MyStarredSong = false;
        RecentPlayFlag = false;
        GroupFlag = false;
        for(AsyncTask myAsyncTask :tasks)
        {
            if (myAsyncTask != null && !myAsyncTask.isCancelled()) {
                myAsyncTask.cancel(true);
            }
        }
        tasks.clear();
        if (!clientList.isEmpty()) {
            for (Call call : clientList) {
                if (call != null && !call.isCanceled()) {
                    call.cancel();
                }
            }
        }
    }

    private void initDatas() {
        mDatas = new ArrayList<>();
        mDatas.add(new ListFragment());


    }
}


